import Vue from 'vue'
import VueRouter from 'vue-router'
//防止重复点击路由 报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
//
import Layout from '../../views/layout/Layout'
import Login from '../../views/Login'
//
import Home from '../../views/Home'
import Home1 from '../../views/Home1'
//系统管理
import User from '../../views/sys/User'
//
Vue.use(VueRouter)

const routers = [{
        path: '/',
        name: '/',
        component: Layout,
        redirect: "/Home",
        children: [{
            path: '/Home',
            name: "1",
            component: Home,
            meta: { title: '首页', pid: 'sub1' },
        }, {
            path: '/Chart',
            name: "2",
            component: Home1,
            meta: { title: '图表', pid: 'sub1' },
        }, {
            path: '/User',
            name: '3',
            component: User,
            meta: { title: '列表Curd', pid: '' },
        }]
    },
    { path: '/Login', name: 'Login', component: Login },
    { path: '*', redirect: "/Home" }
];

const vueRouter = new VueRouter({
    // mode: 'history',
    routes: routers
});

//监听路由
vueRouter.beforeEach((to, from, next) => {
    console.log('路由拦截器', from, to);
    global.tools.loading.start();
    //路由拦截器
    if (to.meta.hasOwnProperty('title')) {
        global.$vuex.commit('vuexApp/add', to);
    }
    return next();
});
vueRouter.afterEach(() => {
    setTimeout(() => {
        global.tools.loading.close();
    });
});

export default vueRouter;